package com.steakliu.sun.loadbalancer;

import com.steakliu.sun.common.meta.ServiceMeta;
import com.steakliu.sun.common.util.RandomUtil;
import com.steakliu.sun.loadbalancer.api.LoadBalancer;
import com.steakliu.sun.spi.annotation.Join;
import org.apache.commons.lang3.ObjectUtils;

import java.util.Collections;
import java.util.List;

/**
 * 功能说明：
 * <p>
 * Original @Author: steakliu-刘牌, 2023-01-07  22:35
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
@Join
public class RandomLoadBalancer implements LoadBalancer {
    @Override
    public ServiceMeta select(List<ServiceMeta> services) {
        if (ObjectUtils.isEmpty(services)){
            return null;
        }
        int size = services.size();
        int index = RandomUtil.random(size);
        return services.get(index);
    }
}
